package transfer.dao;

import transfer.entity.Account;
import transfer.utils.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/*
杨威
2021/9/6
19:52
*/
public class AccountDaoImpl {
    public Account select(String cardNum){
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        Account account=null;
        try {
            connection = DbUtil.getConnection();
            System.out.println(connection);
            preparedStatement=connection.prepareStatement("select password,balance from account where cardnum=?");
            preparedStatement.setString(1,cardNum);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                String password=resultSet.getString(1);
                Integer balance=resultSet.getInt(2);
                account=new Account();
                account.setCardNum(cardNum);
                account.setPassWord(password);
                account.setBalance(balance);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtil.closeAll(null,preparedStatement,resultSet);
        }
        return account;
    }
    public int update(Account account){
        Connection connection = null;
        PreparedStatement preparedStatement=null;
        try {
            connection=DbUtil.getConnection();
            System.out.println(connection);
            preparedStatement = connection.prepareStatement("update account set balance=? where cardnum=?");

            preparedStatement.setInt(1,account.getBalance());
            preparedStatement.setString(2,account.getCardNum());
            int i = preparedStatement.executeUpdate();
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtil.closeAll(null,preparedStatement,null);
        }
        return 0;
    }
}
